var cpfl1Json="";
var wwlbJson="";
$(document).ready(function(){
	getUserRight();
	$("#functionNav").attr("href",navAddr);
	//获取并填充来源类型
	var lylxJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_lyfs'});
	loadSelect("#form1 select[name=lylx]",lylxJson.rows,'id','name');
	//获取并填充时代
	//获取并填充藏品分类
	getSdOption();
	//var doms_sd = ["DJH_SD1","DJH_SD2","DJH_SD3","DJH_SD4"];
	//selectFour(url_nd,doms_sd);
	//获取查询条件
	var searchParams =$("#form1").serializeObject();
	//获取藏品分类1
	cpfl1Json = ajaxResult("get",false,ProjectName+"/sysmanage.getIdNameByParentid.do",{'tableName':'jb_cplb','parentid':0});
	//获取并填充文物普查类别
	wwlbJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do",{'tableName':'jb_wwlb'});
	//初始化待登编Table	
	createTable('#dbTable',ProjectName+'/colmanage.getDbCpListByVistor.do','id',tblColTitle,searchParams);		
  	 //点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#dbTable','#form1');
		 var ztbz = $("#ztbz").val();
		 if(ztbz==6){
			 $("#fpsys_btn").hide();
			 $("#tjfpkf_btn").hide();
			 $("#del_btn_batch").hide();
		 }else{
			 $("#fpsys_btn").show();
			 $("#tjfpkf_btn").show();
			 $("#del_btn_batch").show();
		 }
	 });
	 $("#dykp_btn").click(function(){
		alertFunc("功能正在开发中！");
	 });
	 
	 //打开分配摄影师窗口
	 $("#fpsys_btn").click(function(){
		 var ids = new Array();
		 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));
			 } 
		}); 			
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}
		$("#form_fpsys input[name=ids]").val(ids);
		var sysJson = ajaxResult("get",false,ProjectName+"/root.getUserListByRole.do",{'roleId':'sheyingshi'});
		if(sysJson.status=="ok"){
			loadSelect("#form_fpsys select[name=sysid]",sysJson.rows,'userid','username');
			$("#fpsysModal").modal('show');
		}else{
			alertFunc("获取摄影师列表失败！");
		}
	 });
	 
	 //点击分配摄影师提交按钮
	 $("#btn_add_fpsys").click(function(){
		 if(checkform("#form_fpsys")){
			 var params_sys =$("#form_fpsys").serializeObject();
			 params_sys["sysmc"] = $("#form_fpsys select[name=sysid]").find("option:selected").text();
			 var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.fenpeiShysh.do",params_sys);
			   if(dbResult["status"] == 'ok'){
				   alertFunc("分配摄影师成功！");
				   $("#fpsysModal").modal('hide');
				   refreshTable('#dbTable','#form1');
			   }else{
				   alert(dbResult["error"]);
			   }
		 }
	 });
	 
	 //点击提交分配库房按钮
	 $("#tjfpkf_btn").click(function(){
		 var ids = new Array();
		 var zdjhsFlag = true;
		 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));
				 var zdjh = ($(this).attr('zdjh'));
				 if(zdjh=="" || zdjh==null || zdjh==undefined){
					 zdjhsFlag = false;
					 return false;
				 }					 
			 } 
		}); 		
	    if(!zdjhsFlag){
			alertFunc("请登编后再提交！");
			return false;
		}
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}
		bootbox.setLocale("zh_CN");
		bootbox.confirm("提交后进入分配库房操作，您确定提交吗？",function(r){
			if(r){
				var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.tijiaoDbFenpeiKf.do",{'ids':ids+""});
				if(dbResult["status"] == 'ok'){
				   alertFunc("提交成功！");
				   refreshTable('#dbTable','#form1');
			    }else{
				   alert(dbResult["error"]);
			    }
			}
		});
	 });	
	 

	 $("#btn_copy").click(function(){

		 var params =$("#form_copycount").serializeObject();
		var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.copyData.do",params);
		if(dbResult["status"] == 'ok'){
		   alertFunc("提交成功！");
			$("#copycountModal").modal('hide');
		   refreshTable('#dbTable','#form1');
	    }else{
		   alert(dbResult["error"]);
	    }

	 });	
		
	 $("#del_btn_batch").click(function(){
		delData_batch(); 
	 });
})
function copyData(id){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("确定要复制该藏品吗？",function(r){
		if(r){
			$("#copyids").val(id)
			$("#copycountModal").modal('show');
		}
	});
}

function renewData(id){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("确定要恢复该藏品吗？",function(r){
		if(r){
			var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.renewData.do",{'ids':id+""});
			if(dbResult["status"] == 'ok'){
			   alertFunc("提交成功！");
			   refreshTable('#dbTable','#form1');
		    }else{
			   alert(dbResult["error"]);
		    }
		}
	});
}

function delData_batch(){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("您确定要删除藏品吗？",function(r){
		if(r){

			var ids = new Array();
			 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
				 if ("checked" == $(this).attr("checked")) { 
					 ids.push($(this).attr('value'));
				 } 
			}); 		
			if(ids.length == 0){
				alertFunc("请选择记录！");
				return false;
			}
			delData(ids);
		}
	});
}

function delData_One(id){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("您确定要删除藏品吗？",function(r){
		if(r){
			delData(id);
		}
	});
}
function delData(ids){
	var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.delData.do",{'ids':ids+""});
	if(dbResult["status"] == 'ok'){
		alertFunc("提交成功！");
		refreshTable('#dbTable','#form1');
	}else{
		alert(dbResult["error"]);
	}
}

//更新总登记号
function setZdjh(flhobj){
	var params = new Object();
	params["id"] = $(flhobj).attr('id');	
	params["zdjh"] = $(flhobj).val();
	if(params.id != null || params.id != ""){
		result = ajaxResult("post",false,ProjectName+'/colmanage.updateDbZdjh.do',params);
		if(result["status"] == 'ok'){
			refreshTable('#dbTable','#form1');
		}else{
			alertFunc(result["error"]);
		}
	}
}
//更新藏品分类
function setCpfl(flhobj){
	var params = new Object();
	params["id"] = $(flhobj).attr('id');	
	params["cpfl1"] = $(flhobj).val();
	if(params.id != null || params.id != ""){
		result = ajaxResult("post",false,ProjectName+'/colmanage.updateDbCpfl.do',params);
		if(result["status"] == 'ok'){
			refreshTable('#dbTable','#form1');
		}else{
			alertFunc(result["error"]);
		}
	}
}
//更新文物类别
function setWwlb(flhobj){
	var params = new Object();
	params["id"] = $(flhobj).attr('id');	
	params["cplb"] = $(flhobj).val();
	if(params.id != null || params.id != ""){
		result = ajaxResult("post",false,ProjectName+'/colmanage.updateDbWwlb.do',params);
		if(result["status"] == 'ok'){
			refreshTable('#dbTable','#form1');
		}else{
			alertFunc(result["error"]);
		}
	}
}

var tblColTitle = [{
		 field: 'id',
		 title: '锁定',
		 class:'bs-checkbox',
		 formatter: function(value, row, index) {
			 var result ='';
			 if(row.ztbz==1){
				 if(row.sdbz==1){
					 if(row.sdrid == userInfo.userid){
						 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" checked="checked" onclick="dbrkUnLock('+row.id+')">';
					 }else{
						 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" disabled=true>';
					 }
				 }else{
					 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" onclick="dbrkLock('+row.id+')">';
				 }
			 }else{
				 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" disabled=true>';
			 }
       	  	return result;
       	  }
	    },{
	        field: 'rgh',
	        title: '入馆号'
	    }, {
	        field: 'zdjh',
	        title: '总登记号',
	        formatter: function(value, row, index) {
	        	var result="";	  
	        	if(row.sdbz==1 && row.sdrid == userInfo.userid && row.ztbz==1){
	        		result="<input type='text' style='width:80px' value='"+row.zdjh+"' id='"+row.id+"' onblur='javascript:setZdjh(this)'/>";
	        	}else{
	        		result=row.zdjh;
	        	}           			
	        	return result;
		      }
	    },{
	        field: 'cpfl1',
	        title: '藏品分类',
	        formatter: function(value, row, index) {
	        	var result="";	  
	        	if(row.sdbz==1 && row.sdrid == userInfo.userid && row.ztbz==1){
	        		result="<select id='"+row.id+"' onchange='javascript:setCpfl(this)'>";
	        		result+="<option value=''>请选择</option>";	        		
        			$.each(cpfl1Json.rows,function(index,content){
        				if(row.cpfl1==content["id"]){
        					result +="<option value="+content["id"]+" selected = 'selected'>"+content["name"]+"</option>";
        				}else{
        					result +="<option value="+content["id"]+">"+content["name"]+"</option>";
        				}
        			})
	        		result+="</select>";
	        	}else{
	        		result=row.cpfl1name;
	        	}           			
	        	return result;
		      }
	    },{
	        field: 'cplb',
	        title: '文物普查类别',
	        formatter: function(value, row, index) {
	        	var result="";	  
	        	if(row.sdbz==1 && row.sdrid == userInfo.userid && row.ztbz==1){
	        		result="<select id='"+row.id+"' onchange='javascript:setWwlb(this)'>";
	        		result+="<option value=''>请选择</option>";	        		
        			$.each(wwlbJson.rows,function(index,content){
        				if(row.cplb==content["id"]){
        					result +="<option value="+content["id"]+" selected = 'selected'>"+content["name"]+"</option>";
        				}else{
        					result +="<option value="+content["id"]+">"+content["name"]+"</option>";
        				}
        			})
	        		result+="</select>";
	        	}else{
	        		result=row.cplbname;
	        	}           			
	        	return result;
		      }
	    },{
	        field: 'cpmc',
	        title: '藏品名称',
	        formatter: function(value, row, index) {	     
	        	var result="";	  
    			if(row.ztbz==1 && row.sdbz==1 && row.sdrid == userInfo.userid)	   
        			result = '<a href="dbDetail.html?id='+row.id+'">'+value+'</a>';
    			else
    				result =value;
	        	return result;	        	
		      }
	    }, {
	        field: 'sd4',
	        title: '时代',
	        formatter: function(value, row, index) {
	        	var sd="";
	        	if(row.sd4!="" && row.sd4!=null){
	        		sd =row.sd4;
	        	}else if(row.sd3!="" && row.sd3!=null){
	        		sd =row.sd3;
	        	}else if(row.sd2!="" && row.sd2!=null){
	        		sd =row.sd2;
	        	}else if(row.sd1!="" && row.sd1!=null){
	        		sd =row.sd1;
	        	}
	        	return sd;
		      }
	    }, {
	        field: 'sjsl',
	        title: '实际数量',
	        formatter: function(value, row, index) {	        	
	        	return value+row.sjsldw;
		      }
	    }, {
	        field: 'lylxmc',
	        title: '来源类型'
	    }, {
	        field: 'sdrmc',
	        title: '锁定人',
	        formatter: function(value, row, index) {
	        	  if(row.sdbz==1)
	        		  return value;
	        	  else
	        		  return "";
		      	}
	    },{
	        field: 'ztbz',
	        title: '所在阶段',
	        formatter: function(value, row, index) {
	        	  if(value==1)
	        		  return "登编";
	        	  else if(value==2)
	        		  return "上传影像";
	        	  else if(value==3)
	        		  return "分配库房";
	        	  else if(value==4)
	        		  return "入库";
	        	  else if(value==5)
	        		  return "完成";
	        	  else if(value==6)
	        		  return "删除";
	        	  else
	        		  return "";	        		  
		      	}
	    },{
	        field: '',
	        title: '操作',
	        formatter: function(value, row, index) {
	        	var result="";	  
	        	if(row.sdbz==1 && row.sdrid == userInfo.userid &&row.scbz==1){
	        		result = '<a href="#" onclick="renewData(\''+row.id+'\')">恢复</a>';
	        	}
	        	else if(row.sdbz==1 && row.sdrid == userInfo.userid && row.ztbz==1)	   
        			result = '<a href="dbInfo.html?id='+row.id+'">登编</a>&nbsp;'
        			+'<a href="#" onclick="copyData(\''+row.id+'\')">复制</a>&nbsp;'
        			+'<a href="#" onclick="delData_One(\''+row.id+'\')">删除</a>';
	        	return result;
		      }
	    }];
